package com.tianjin.weight.utils;

import cn.hutool.core.date.LocalDateTimeUtil;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;

public class LocalDateUtils {

    /**
     * @Author guss
     * @Description 获取当前周第一天的第一刻
     * @Date 2021/4/15 12:42
     * @Param [now]
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime getStartDayOfWeek(LocalDateTime now){
        if(null == now){
            now = LocalDateTime.now();
        }
        TemporalField fieldIso = WeekFields.of(DayOfWeek.MONDAY, 1).dayOfWeek();
        now = now.with(fieldIso, 1);
        LocalDateTime localDateTime = LocalDateTimeUtil.beginOfDay(now);
        return localDateTime;
    }

    /**
     * @Author guss
     * @Description 获取当前周的最后一天的最后一刻a
     * @Date 2021/4/15 12:43
     * @Param [now]
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime getEndDayOfWeek(LocalDateTime now){
        if(null == now){
            now = LocalDateTime.now();
        }
        TemporalField fieldIso = WeekFields.of(DayOfWeek.MONDAY, 1).dayOfWeek();
        now = now.with(fieldIso, 7);
        LocalDateTime localDateTime = LocalDateTimeUtil.endOfDay(now);
        return localDateTime;
    }

    /**
     * @Author guss
     * @Description 获取当前月第一天的第一刻
     * @Date 2021/4/15 12:57
     * @Param [now]
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime getStartDayOfMonth(LocalDateTime now){
        if(null == now){
            now = LocalDateTime.now();
        }
        return LocalDateTimeUtil.beginOfDay(now.with(TemporalAdjusters.firstDayOfMonth()));

    }

    /**
     * @Author guss
     * @Description 获取当前月最后一天的最后一刻
     * @Date 2021/4/15 12:57
     * @Param [now]
     * @return java.time.LocalDateTime
     **/
    public static LocalDateTime getEndDayOfMonth(LocalDateTime now){
        if(null == now){
            now = LocalDateTime.now();
        }
        return LocalDateTimeUtil.endOfDay(now.with(TemporalAdjusters.lastDayOfMonth()));
    }

}
